﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BloggerAutoPosterService.Engine.Models;
using BloggerAutoPosterService.Engine.Utils;

namespace BloggerAutoPosterService.Engine.Core
{
    public class ExportData
    {
        #region Fields (1)

        readonly IServiceConfig _serviceConfig;

        #endregion Fields

        #region Constructors (1)

        public ExportData(IServiceConfig serviceConfig)
        {
            _serviceConfig = serviceConfig;
        }

        #endregion Constructors

        #region Methods (8)

        // Public Methods (3) 

        public string DatesListToHtml(IList<string> dates)
        {
            var todayList = DatesToList(dates);
            if (todayList == null || !todayList.Any()) return string.Empty;
            return ListToString(todayList);
        }

        public IList<NewsRecord> DatesToList(IList<string> dates)
        {
            var query = new XmlDB(_serviceConfig).ListNewsRecords(includeDeletedRecords: false);
            if (query == null || !query.Any()) return null;
            return query.Where(x => dates.Contains(x.AddDate) && !x.Sent).ToList();
        }

        public string ListToString(IList<NewsRecord> todayList)
        {
            if (todayList == null) return string.Empty;

            var farsiItemsList = todayList.Where(x => x.Title.ContainsFarsi()).OrderBy(x => x.Title).ToList();
            var englishItemsList = todayList.Where(x => !x.Title.ContainsFarsi()).OrderBy(x => x.Annotation).ThenBy(x => x.Title).ToList();

            var farsiHtmlList = getHtmlList(farsiItemsList);
            var englishHtmlList = getHtmlList(englishItemsList);

            var align = string.Empty;
            if (_serviceConfig.UsePersianDate)
                align = "align='right' dir='rtl'";

            return string.Format("<div {0}><br/>{1}{2}</div>", align, farsiHtmlList, englishHtmlList).ApplyUnifiedYeKe();
        }
        // Private Methods (5) 

        private static string getAuthor(NewsRecord entry)
        {
            if (string.IsNullOrWhiteSpace(entry.Author)) return string.Empty;

            var author = entry.Author.RemoveEmailPart().HtmlDecode();
            if (author.ToLowerInvariant().Contains("author unknown")) return string.Empty;

            if (!string.IsNullOrWhiteSpace(author))
            {
                var dir = getDir(author);
                author = "<span " + dir + ">" + author + "</span> | ";
            }
            return author;
        }

        private static string getDir(string data)
        {
            var isRtl = data.ContainsFarsi();
            var dir = "dir='ltr'";
            if (isRtl) dir = "dir='rtl'";
            return dir;
        }

        private static string getHtmlList(IEnumerable<NewsRecord> list)
        {
            var sb = new StringBuilder();
            foreach (var entry in list)
            {
                var title = getTitle(entry);
                var dir = getDir(title);
                var author = getAuthor(entry);
                var urlHost = getUrlHost(entry);

                sb.AppendFormat("<ul><li><a {0} href='{1}'>{2}</a> <span style='color:darkgray'>|</span> <span style='color:gray'>{4}{3}</span> </li></ul>",
                    dir,
                    entry.Link.TrimFeedburnerPart().EscapeXml(),
                    title,
                    urlHost,
                    author);
            }
            return sb.ToString();
        }

        private static string getTitle(NewsRecord entry)
        {
            var title = entry.Title.HtmlDecode().EscapeXml();
            if (!string.IsNullOrWhiteSpace(entry.Annotation))
            {
                title = entry.Annotation.HtmlDecode().EscapeXml();
            }
            return title;
        }

        private static string getUrlHost(NewsRecord entry)
        {
            return string.Format("<span dir='ltr'>{0}</span>", new Uri(entry.Link).Host);
        }

        #endregion Methods
    }
}
